package com.java01.apache.commons.exec;

import java.io.IOException;

import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;
import org.apache.commons.exec.LogOutputStream;
import org.apache.commons.exec.PumpStreamHandler;

public class LogOutputStreamSnippet {

	public static void main(String[] args) {
		new LogOutputStreamSnippet();
	}
	
	public LogOutputStreamSnippet(){
		DefaultExecutor executor = new DefaultExecutor();

		OutputMonitor outputMonitor = new OutputMonitor();

		PumpStreamHandler streamHandler = new PumpStreamHandler(outputMonitor);
		executor.setStreamHandler(streamHandler);

		CommandLine commandLine = CommandLine.parse("ipconfig");

		try {
			int exitCode = executor.execute(commandLine);
			System.out.println("exit "+exitCode);
		} catch (ExecuteException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public class OutputMonitor extends LogOutputStream{
		@Override
		protected void processLine(String line, int level) {
			System.out.println(">>>"+line);
		}
	}
}